package com.edgclub.edg.api;

import android.content.Context;
import android.text.TextUtils;

import com.edgclub.edg.GuZhiApplication;
import com.edgclub.edg.api.param.AttentionUsrtParam;
import com.edgclub.edg.api.param.CheckUserReportParam;
import com.edgclub.edg.api.param.ReportUserParam;
import com.edgclub.edg.api.param.UserParam;
import com.edgclub.edg.http.VolleyListener;
import com.edgclub.edg.http.async.AsyncHttpClient;
import com.edgclub.edg.http.async.AsyncHttpResponseHandler;
import com.edgclub.edg.http.async.RequestParams;
import com.edgclub.edg.http.async.ResponseHandlerInterface;
import com.edgclub.edg.utils.util.CryptUtil;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

/**
 * 用户相关操作的api
 * 
 * @author 陈坤彬
 *
 */
public class UserApi extends GuZhiApi {

	private AsyncHttpClient asyncHttpClient;

	public UserApi(Context mContext) {
		super(mContext);
		asyncHttpClient = new AsyncHttpClient();
	}


	/**
	 * 注册(手机号)
	 *
	 * @param params
	 * @param listener
	 */
	public void registerPhone(UserParam params, VolleyListener listener) {
		post(ApiConfig.APIURL.URL_REGISTER_PHONE,
				ApiConfig.APIURL.URL_REGISTER_PHONE, params, listener);
	}
	/**
	 *忘记密码接口
	 *
	 * @param params
	 * @param listener
	 */
	public void forgetPsw(UserParam params, VolleyListener listener) {
		post(ApiConfig.APIURL.URL_FORGET_PSW,
				ApiConfig.APIURL.URL_FORGET_PSW, params, listener);
	}
	/**
	 * 个人消息通知设置
	 *
	 * @param params
	 * @param listener
	 */
	public void personMessage(UserParam params, VolleyListener listener) {
		post(ApiConfig.APIURL.URL_PERSON_MESSAGE,
				ApiConfig.APIURL.URL_PERSON_MESSAGE, params, listener);
	}

	/**
	 * 个人消息通知设置
	 *
	 * @param params
	 * @param listener
	 */
	public void reportMessage(UserParam params, VolleyListener listener) {
		post(ApiConfig.APIURL.URL_REPORT_MSGSET,
				ApiConfig.APIURL.URL_REPORT_MSGSET, params, listener);
	}


	/**
	 * 获取验证码
	 *
	 * @param params
	 * @param listener
	 */
	public void registerGetcode(UserParam params, VolleyListener listener) {
		post(ApiConfig.APIURL.URL_REGISTER_CODE,
				ApiConfig.APIURL.URL_REGISTER_CODE, params, listener);
	}
	/**
	 * 验证验证码
	 *
	 * @param params
	 * @param listener
	 */
	public void registerCode(UserParam params, VolleyListener listener) {
		post(ApiConfig.APIURL.URL_REGISTER_CODE1,
				ApiConfig.APIURL.URL_REGISTER_CODE1, params, listener);
	}

	/**
	 * 绑定手机号
	 *
	 * @param params
	 * @param listener
	 */
	public void boundPhone(UserParam params, VolleyListener listener) {
		post(ApiConfig.APIURL.URL_BOUND_PHONE,
				ApiConfig.APIURL.URL_BOUND_PHONE, params, listener);

	}

	/**
	 * 检查是否已绑定第三方
	 * 
	 * @param params
	 * @param listener
	 */
	public void checkBind(UserParam params, VolleyListener listener) {
		post(ApiConfig.APIURL.URl_CHECK_BIND, ApiConfig.APIURL.URl_CHECK_BIND,
				params, listener);
	}

	/**
	 * 绑定
	 * 
	 * @param params
	 * @param listener
	 */
	public void userBind(UserParam params, VolleyListener listener) {
		post(ApiConfig.APIURL.URl_USER_BIND,
				ApiConfig.APIURL.URl_USER_BIND, params, listener);
	}


	/**
	 * 登陆
	 * 
	 * @param params
	 * @param listener
	 */
	public void login(UserParam params, VolleyListener listener) {
		post(ApiConfig.APIURL.URL_LOGIN, ApiConfig.APIURL.URL_LOGIN, params,
				listener);
	}
	/**
	 * 注册环信用户（前端发现用户登录环信时，返回用户不存在状态码，调用该方法进行注册）
	 *
	 * @param params
	 * @param listener
	 */
	public void registerHx(UserParam params, VolleyListener listener) {
		post(ApiConfig.APIURL.URL_REGISTERHX, ApiConfig.APIURL.URL_REGISTERHX, params,
				listener);
	}

	/**
	 * 添加签名
	 * 
	 * @param inputStream
	 * @param address
	 * @param content
	 * @param fileHandler
	 */
	public void addUserSign(InputStream inputStream, String address,
			String content, String userId, AsyncHttpResponseHandler fileHandler) {

		RequestParams params = new RequestParams();
		params.put("userId", userId);
		if (null != inputStream) {
			params.put("inputStream", inputStream);
		}
		params.put("content", content);
		if (null != address && !TextUtils.isEmpty(address)) {
			params.put("address", address);
		}

		String t = System.currentTimeMillis() + "";

		params.put("t", t);

		if (null == address || TextUtils.isEmpty(address)) {
			params.put(
					"sign",
					CryptUtil.md5("content=" + content + "&t=" + t + "&userId="
							+ userId + "&key=" + ApiConfig.HTTP_KEY));
		} else {
			params.put(
					"sign",
					CryptUtil.md5("address=" + address + "&content=" + content
							+ "&t=" + System.currentTimeMillis() + "&userId="
							+ userId + "&key=" + ApiConfig.HTTP_KEY));
		}

		asyncHttpClient.post(ApiConfig.BASE_URL
				+ ApiConfig.APIURL.URL_ADDUSERSIGN, params, fileHandler);
	}


	/**
	 * 用户上传头像
	 * 
	 * @param file
	 * @param handler
	 */
	public void userUpload(File file,
						 ResponseHandlerInterface handler) {
		RequestParams params = new RequestParams();
		params.put("userId", GuZhiApplication.getInstance().getUserId());
		try {
			params.put("logo", file);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		params.put("sign",CryptUtil.sign(params,ApiConfig.HTTP_KEY));
		asyncHttpClient.post(ApiConfig.BASE_URL
				+ ApiConfig.APIURL.URL_USERIPLOAD, params, handler);

	}

	/**
	 * 替换相册图片
	 * 
	 * @param userId
	 * @param crc
	 * @param inputStream
	 */
	public void updateUserImages(String userId, String crc,
			FileInputStream inputStream, AsyncHttpResponseHandler handler) {
		RequestParams params = new RequestParams();

		params.put("userId", userId);
		params.put("crc", crc);
		params.put("inputStream", inputStream);

		params.put("t", System.currentTimeMillis() + "");
		params.put(
				"sign",
				CryptUtil.md5("crc=" + crc + "&t=" + System.currentTimeMillis()
						+ "&userId=" + userId + "&key=" + ApiConfig.HTTP_KEY));
		asyncHttpClient.post(ApiConfig.BASE_URL
				+ ApiConfig.APIURL.URL_UPDATEUSERIMAGE, params, handler);
	}



	/**
	 * 验证手机
	 * 
	 * @param param
	 * @param listener
	 */
	public void verifyPhone(UserParam param, VolleyListener listener) {
		post(ApiConfig.APIURL.URL_VERIFYPHONE,
				ApiConfig.APIURL.URL_VERIFYPHONE, param, listener);
	}

	/**
	 * 验证验证码
	 * 
	 * @param param
	 * @param listener
	 */
	public void verifyCode(UserParam param, VolleyListener listener) {
		post(ApiConfig.APIURL.URL_VERIFYCODE, ApiConfig.APIURL.URL_VERIFYCODE,
				param, listener);
	}

	/**
	 * 注册的验证码
	 * 
	 * @param param
	 * @param listener
	 */
	public void registerSms(UserParam param, VolleyListener listener) {
		post(ApiConfig.APIURL.URL_REGISTERSMS,
				ApiConfig.APIURL.URL_REGISTERSMS, param, listener);
	}

	/**
	 * 第三方登录跳到注册--获取验证码
	 * 
	 * @param param
	 * @param listener
	 */
	public void registerSmsByThird(UserParam param,
			VolleyListener listener) {
		post(ApiConfig.APIURL.URl_GET_CODE, ApiConfig.APIURL.URl_GET_CODE,
				param, listener);
	}

	/**
	 * 第三方登录跳到注册--获取验证码
	 *
	 * @param param
	 * @param listener
	 */
	public void registerSmsByShop(UserParam param,
								  VolleyListener listener) {
		post(ApiConfig.APIURL.URl_GET_CODE, ApiConfig.APIURL.URl_GET_CODE,
				param, listener);
	}
	/**
	 * 个人主页信息接口
	 *
	 * @param param
	 * @param listener
	 */
	public void gethomepage(UserParam param,
								  VolleyListener listener) {
		post(ApiConfig.APIURL.URL_HOMEPAGE, ApiConfig.APIURL.URL_HOMEPAGE,
				param, listener);
	}
	/**
	 * ta个人主页信息接口
	 *
	 * @param param
	 * @param listener
	 */
	public void getotherhomepage(UserParam param,
							VolleyListener listener) {
		post(ApiConfig.APIURL.URL_OTHERHOMEPAGE, ApiConfig.APIURL.URL_OTHERHOMEPAGE,
				param, listener);
	}

	/**
	 * 查看TA的图片接口
	 *
	 * @param param
	 * @param listener
	 */
	public void getOtherImage(UserParam param,
							VolleyListener listener) {
		post(ApiConfig.APIURL.URL_USER_IMAGE, ApiConfig.APIURL.URL_USER_IMAGE,
				param, listener);
	}
	/**
	 * 个人相册列表接口
	 *
	 * @param param
	 * @param listener
	 */
	public void getOtherPhoto(UserParam param,
							  VolleyListener listener) {
		post(ApiConfig.APIURL.URL_USER_PHOTO, ApiConfig.APIURL.URL_USER_PHOTO,
				param, listener);
	}

	/**
	 * 删除相册接口
	 *
	 * @param param
	 * @param listener
	 */
	public void getDelImage(UserParam param,
							  VolleyListener listener) {
		post(ApiConfig.APIURL.URL_USER_DELIMAGE, ApiConfig.APIURL.URL_USER_DELIMAGE,
				param, listener);
	}

	/**
	 * 勋章荣誉信息接口
	 *
	 * @param param
	 * @param listener
	 */
	public void gethonour(UserParam param,
							VolleyListener listener) {
		post(ApiConfig.APIURL.URL_HONOUR, ApiConfig.APIURL.URL_HONOUR,
				param, listener);
	}
	/**
	 * 修改密码接口
	 *
	 * @param param
	 * @param listener
	 */
	public void getpassword(UserParam param,
							VolleyListener listener) {
		post(ApiConfig.APIURL.URL_PASSWORD, ApiConfig.APIURL.URL_PASSWORD,
				param, listener);
	}
	/**
	 * 修改昵称接口
	 *
	 * @param param
	 * @param listener
	 */
	public void getupname(UserParam param,
							VolleyListener listener) {
		post(ApiConfig.APIURL.URL_CHANGE_NAME, ApiConfig.APIURL.URL_CHANGE_NAME,
				param, listener);
	}
	/**
	 * 修改简介接口
	 *
	 * @param param
	 * @param listener
	 */
	public void getupsing(UserParam param,
							VolleyListener listener) {
		post(ApiConfig.APIURL.URL_SING_NAME, ApiConfig.APIURL.URL_SING_NAME,
				param, listener);
	}
	/**
	 * 用户注册完善个人信息接口
	 *
	 */
	public void getUpdateInfo(RequestParams params,
							  ResponseHandlerInterface handler) {
			params.put("sign",CryptUtil.sign(params,ApiConfig.HTTP_KEY));
			asyncHttpClient.post(ApiConfig.BASE_URL
					+ ApiConfig.APIURL.URL_USER_UPDATEINFO, params, handler);

	}
	/**
	 * 用户注册完善个人信息接口
	 *
	 * @param param
	 * @param listener
	 */
	public void getUpdateInfopost(UserParam param,
						  VolleyListener listener) {
		post(ApiConfig.APIURL.URL_USER_UPDATEINFO, ApiConfig.APIURL.URL_USER_UPDATEINFO,
				param, listener);
	}

	/**
	 * 检测用户举报
	 *
	 * @param params
	 * @param listener
	 */
	public void checkUSerReport(CheckUserReportParam params,
								VolleyListener listener) {
		post(ApiConfig.APIURL.URL_CHECKUSERREPORT,
				ApiConfig.APIURL.URL_CHECKUSERREPORT, params, listener);
	}

	/**
	 * 太多用户举报了
	 *
	 * @param param
	 * @param listener
	 */
	public void moreUserReport(AttentionUsrtParam param, VolleyListener listener) {
		post(ApiConfig.APIURL.URL_ATTENTIONUSER,
				ApiConfig.APIURL.URL_ATTENTIONUSER, param, listener);
	}

	/**
	 *
	 * @param params
	 * @param listener
	 */
	public void reportUser(ReportUserParam params, VolleyListener listener) {
		post(ApiConfig.APIURL.URL_USERREPORT,
				ApiConfig.APIURL.URL_USERREPORT, params, listener);
	}

	/**
	 * 取消请求
	 * 
	 * @param context
	 */
	public void cancelAsyncHttp(Context context) {
		asyncHttpClient.cancelRequests(context, true);
	}

	/**
	 * 封用户和设备接口
	 *
	 * @param param
	 * @param listener
	 */
	public void forbidden(UserParam param,
								 VolleyListener listener) {
		post(ApiConfig.APIURL.URL_FORBIDDEN, ApiConfig.APIURL.URL_FORBIDDEN,
				param, listener);
	}
}
